<?php
namespace App\Api\Controllers\Weixin;



use App\Api\Controllers\Config\CibPayConfigController;
use App\Api\Controllers\Config\WingConfigController;
use App\Api\Controllers\WftPay\PayController as WftpayPayController;
use App\Models\WingStores;
use Illuminate\Http\Request;
use App\Models\Store;

class UserController extends BaseController
{
    public function index()
    {

    }
    public function get_openid(Request $request)
    {
        $code = $request->input('code');
        return ['openid' => 'oPszD6yRDOC_sF5R_uMKIrdGBOVg'];
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=wxdd9213223d01c769&secret=4ecce160ec92243632053b5a2be813f5&js_code='.$code.'&grant_type=authorization_code';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"GET");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $temp = curl_exec($ch);
        return json_decode($temp, true);
    }

    public function pay (Request $request)
    {
        $store_id = $request->input('store_id');
        $open_id = $request->input('open_id');
        $out_trade_no = 'wx_mini' . date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
        $config = \App\Models\WeixinStore::where(['store_id' => $store_id])->first();

        $wftPayConfig = new CibPayConfigController();
        $cib_merchant = $wftPayConfig->cib_merchant($store_id, '');
        if (!$cib_merchant) {
            return json_encode([
                'status' => 2,
                'message' => '商户未成功开通威富通通道'.$store_id.$open_id
            ]);
        }

        $wftpay_config = $wftPayConfig->wftpay_config(1234);
        if (!$wftpay_config) {
            return json_encode([
                'status' => 2,
                'message' => '威富通支付配置不存在请检查配置'
            ]);
        }
        $obj = new WftpayPayController();
        $wxParams = [
            'mch_id'        => $cib_merchant->mch_id,
            'is_raw'        => 1,
            'is_minipg'     => 1,
            'out_trade_no'  => $out_trade_no,
            'body'          => '小程序支付',
            'sub_openid'    => $open_id,
            'sub_appid'     => $config->wx_shop_id,
            'total_amount'     => 0.01,
            'private_rsa_key' => $wftpay_config->private_rsa_key,
            'public_rsa_key'  => $wftpay_config->public_rsa_key,
        ];
        return $obj->qr_submit($wxParams); //0-系统错误 1-成功 2-失败
    }

    public function betapay(Request $request)
    {
        $mchId = $request->input('mch_id');
        $mchInfo = WingStores::where('merchantNo', $mchId)->first();
        if (!$mchInfo) {
            return json_encode([
                'status' => 2,
                'message' => '信息不存在'
            ]);
        }
        $store_id = $mchInfo->store_id;
        $obj = new \App\Api\Controllers\lianfu\PayController();
        $config = new WingConfigController();
        $h_merchant = $config->wing_merchant($store_id, '');
        if (!$h_merchant) {
            return json_encode([
                'status' => 2,
                'message' => '翼支付商户号不存在'
            ]);
        }

        $data1 = [
            'agreeId' => '20201020030100057964525181206529',
            'signType' => 'S002',
            'signMerchantNo' => $h_merchant->institutionCode,
            'merchantCode' => $h_merchant->merchantNo,
            'terminalCode' => $store_id

        ];

        $storeInfo = Store::where('store_id', $store_id)->first();

        $terminalPutAddress = $storeInfo->province_name.'-'.$storeInfo->city_name.'-'.$storeInfo->area_name.'-'.$storeInfo->store_address;
        $data2 = [
            'agreeId' => '20201020030100057964525181206529',
            'signType' => 'S002',
            'outTradeNo' => $store_id.'6666',
            'signMerchantNo' => $h_merchant->institutionCode,
            'merchantCode' => $h_merchant->merchantNo,
            'terminalCode' => $store_id,
            'terminalFirmName' => '扫码盒子',//终端厂家名称
            'terminalType' => '11',
            'terminalModelNo' => 'sl51',
            'terminalPutDate' => date('Y-m-d H:i:s'),
            'terminalSourceChannel' => 'EXTERNAL',
            'gpsFlag' => '02',
            'terminalPutAddress' => $terminalPutAddress
        ];

        $return1 = $obj->scan_pay22($data1, 'https://openapi.qdlpay.com/gapi/o2o/terminalPlatform/services/queryTerminalBankReportResult');
        $return2 = $obj->scan_pay22($data2, 'https://openapi.qdlpay.com/gapi/o2o/terminalPlatform/services/collectInformation');

        $ret = [
            'collectInformation' => [
                'request' => $data2,
                'response' => $return2
            ],
            'queryTerminalBankReportResult' => [
                'request' => $data1,
                'response' => $return1
            ]
        ];


        return $ret;
    }


    public function test()
    {





//        $bizContent = [
//            'appId' => 10000000000003213678,
//            'outUserId' => ,
//            'notifyUrl' => ,
//            'outOrderId' => ,
//            'goodsName' => ,
//            'trxChannel' => ,
//            'payAmount' => ,
//            'qrCode' => ,
//            'payType' => ,
//            'terminalInfo' => ,
//            'subOrders' => ,
//        ];
//        $params = [
//            'app_id' => '',
//            'msg_id' => uniqid(),
//            'sign' => '',
//            'timestamp' => date('Y-m-d H:i:s'),
//            'biz_content' => $bizContent
//        ];
    }

}
